package com.softWang.system.controller;

import com.softWang.common.aop.annotation.SysOperationLog;
import com.softWang.common.response.PageBean;
import com.softWang.common.request.Idparam;
import com.softWang.common.response.ResponseResult;
import com.softWang.system.constants.SysModel;
import com.softWang.system.controller.vo.systemDepts.*;
import com.softWang.system.po.SystemDepts;
import com.softWang.system.service.SystemDeptsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

@Validated
@RestController
@Api(value="部门管理",tags="部门管理")
@RequestMapping("/system/dept")
public class SystemDeptsController {
    @Resource
    private SystemDeptsService deptsService;


    @ApiOperation(value="部门列表(分页)")
    @PreAuthorize("@ss.hasPermi('system:dept:query')")
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public ResponseResult<PageBean<SystemDeptQueryAllResult>> queryall(@RequestBody @Validated SystemDeptQueryAllParam param, HttpServletRequest request) {
        List<SystemDeptQueryAllResult> depts=deptsService.queryall(param);
        PageBean page=new PageBean(depts);
        return ResponseResult.success(page);
    }

    @ApiOperation(value="添加部门")
    @PreAuthorize("@ss.hasPermi('system:dept:add')")
    @SysOperationLog(value = "添加部门", model = SysModel.DEPT_MANAGE)
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    //@Repeat(message = "重复提交")
    public ResponseResult<Long> add(@RequestBody @Validated SystemDeptsAddParam dept, HttpServletRequest request) throws Exception {
        SystemDepts systemDepts = new SystemDepts();
        BeanUtils.copyProperties(dept, systemDepts);
        Long id = deptsService.add(systemDepts);
        return ResponseResult.success(id);
    }

    @ApiOperation(value="修改部门")
    @PreAuthorize("@ss.hasPermi('system:dept:edit')")
    @SysOperationLog(value = "修改部门", model = SysModel.DEPT_MANAGE)
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public ResponseResult<Long> edit(@RequestBody @Validated SystemDeptsEditParam dept, HttpServletRequest request) {
        SystemDepts systemDepts = new SystemDepts();
        BeanUtils.copyProperties(dept, systemDepts);
        Long id = deptsService.edit(systemDepts);
        return ResponseResult.success(id);
    }

    @ApiOperation(value="查看详情")
    @PreAuthorize("@ss.hasPermi('system:dept:detail')")
    @RequestMapping(value = "/detail", method = RequestMethod.POST)
    public ResponseResult<SystemDeptsDetailResult> detail(@RequestBody @Validated Idparam idparam) {
        SystemDeptsDetailResult dept = new SystemDeptsDetailResult();
        SystemDepts deptsPo = deptsService.getById(idparam.getId());
        BeanUtils.copyProperties(deptsPo, dept);
        return ResponseResult.success(dept);
    }

    @ApiOperation(value="删除部门")
    @PreAuthorize("@ss.hasPermi('system:dept:del')")
    @SysOperationLog(value = "删除部门", model = SysModel.DEPT_MANAGE)
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public ResponseResult del(@RequestBody @NotEmpty(message = "ids 不能为空") List<@NotNull(message = "ids 不能为空") Long> ids, HttpServletRequest request) {
        deptsService.del(ids);
        return ResponseResult.success();
    }

    @ApiOperation(value="获取机构树")
    @RequestMapping(value = "/tree", method = RequestMethod.POST)
    public ResponseResult<List<SystemDeptsTreeResult>> tree(@RequestBody Idparam idparam, HttpServletRequest request) {
        List<SystemDepts> orgs=deptsService.getOrgTree(idparam.getId());
        List<SystemDeptsTreeResult> result = new ArrayList();
        for(SystemDepts dept: orgs){
            SystemDeptsTreeResult treeItem = new SystemDeptsTreeResult();
            BeanUtils.copyProperties(dept, treeItem);
            result.add(treeItem);
        }
        return ResponseResult.success(result);
    }
}
